package com.outstanding.his.patient.facade;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.outstanding.his.doctor.domain.Doctor;
import com.outstanding.his.doctor.service.IDoctorService;
import com.outstanding.his.patient.domain.Patient;
import com.outstanding.his.patient.service.IPatientService;
import com.outstanding.his.util.pager.Pagination;

@Controller
@SessionAttributes("patient")
public class PatientController {

	@Autowired
	private IPatientService patientService;
	private IDoctorService doctorService;

	public void setPatientService(IPatientService patientService) {
		this.patientService = patientService;
	}

	public void setDoctorService(IDoctorService doctorService) {
		this.doctorService = doctorService;
	}

	@RequestMapping("/patient/search.do")
	public String patientSearch(Model model) {
		return "redirect:/patient/list.do";
	}

	@RequestMapping("/patient/list.do")
	public String patientList(Model model) {
		Pagination pagination = new Pagination();
		String hql = "select * from patients";
		
		pagination.setTotalCount(50);
		model.addAttribute("pagination", pagination);
		model.addAttribute("patients", patientService.getPatientList());
		return "/patient/list";
	}

	@RequestMapping("/patient/index.do")
	public String patientIndex() {
		return "/patient/index";
	}

	@RequestMapping("/patient/new.do")
	public String patientNewForm(Model model) {
		Patient patient = new Patient();
		List<Doctor> doctors = (List<Doctor>) doctorService.getDoctorList();
		model.addAttribute("patient", patient);
		model.addAttribute("doctors", doctors);
		return "/patient/new";
	}

	@RequestMapping(value = "/patient/submit.do", method = RequestMethod.POST)
	public String patientSubmit(@ModelAttribute("patient") Patient patient,
			@ModelAttribute("doctor") Doctor doctor, Model model) {
		patient.setDoctor(doctor);
		if (patient.getPatientId() != null) {
			patient = patientService.updatePatient(patient);
		} else {
			patient = patientService.submitPatient(patient);
		}
		model.addAttribute("patient", patient);

		return "redirect:/patient/view.do?patientId=" + patient.getPatientId();
	}

	@RequestMapping(value = "/patient/view.do")
	public String patientView(@RequestParam("patientId") Integer patientId,
			Model model) {
		Patient patient = patientService.getPatient(patientId);
		model.addAttribute(patient);
		return "/patient/view";
	}

	@RequestMapping("/patient/edit.do")
	public String patientEdit(@RequestParam("patientId") Integer patientId,
			Model model) {
		Patient patient = patientService.getPatient(patientId);
		List<Doctor> doctors = (List<Doctor>) doctorService.getDoctorList();
		model.addAttribute("patient", patient);
		model.addAttribute("doctors", doctors);
		return "/patient/edit";
	}
}
